package com.market.dao.jpa;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.market.dao.OrderDao;
import com.market.domain.Order;
import com.market.domain.OrderDetail;
import com.market.domain.Product;
import com.market.domain.Order.STATUS;
import com.market.domain.Promotion;
import com.market.domain.transients.OrderQuery;

@Repository("orderDao")
public class OrderDaoJpa extends GenericDaoJpa<Order, Long> implements OrderDao {

	public OrderDaoJpa() {
		super(Order.class);
	}

	/**
	 * This method override save method in super class, the difference is new
	 * order have the procedure of generating orderNumber and saving it.
	 */
	public Order save(Order order) {
		if (order.getIsNew() == true) {
			order.setOrderedDate(new Date());
			order.setNumber(String.valueOf(new Date().getTime())); // This act
																	// as a
																	// temporary
																	// order
																	// number
			List<Promotion> promotions = order.getPromotions();
			if (promotions != null && promotions.size() > 0) { // Because promotion
														// is detached
														// object, so can
														// not use persist

				order.setPromotions(null);  //Set it to null

			}
			order = super.persist(order);
			SimpleDateFormat formatter = new SimpleDateFormat("yyMMddEEE");
			String required = formatter.format(order.getRequiredDate());
			String number = required + "-" + order.getId();
			order.setNumber(number);
			order.setStatus(STATUS.RECEIVED);
			order.setPromotions(promotions);
			order = super.merge(order);
		} else {
			order = super.merge(order);
		}
		for (OrderDetail detail : order.getOrderDetails()) {
			detail.setIsNew(false);
		}
		return order;
	}

	public void remove(Order object) {
		if (super.findById(object.getId()) == null) {
			log.debug(this.getClass() + " Orders with id " + object.getId()
					+ " is not found, can not be removed");

		} else {
			log.debug(this.getClass() + " Orders with id " + object.getId()
					+ " is found, will be removed");
			super.removeById(object.getId());
		}
	}

	public List<OrderDetail> findOrderDetails(Order order) {
		String sql = "select orderdetail.* from orders_orderdetail,orderdetail where orderdetail.id = orders_orderdetail.orderdetails_id"
				+ " and orders_orderdetail.orders_id = " + order.getId();
		Query q = this.em.createNativeQuery(sql, OrderDetail.class);
		List<OrderDetail> details = q.getResultList();
		return details;
	}

	public List<Promotion> findPromotions(Order order) {
		String sql = "select promotion.* from promotion, orders_promotion where promotion.id = orders_promotion.promotions_id and "
				+ " orders_promotion.orders_id = " + order.getId();
		Query q = this.em.createNativeQuery(sql, Promotion.class);
		List<Promotion> promotions = q.getResultList();
		return promotions;
	}

	public List<Order> findOrders(OrderQuery orderQuery) {
		String sql = "select orders.* from orders,user,location where orders.user_id = user.id and user.dropOffLocation_id = location.id ";
		if (orderQuery.getStatus() != Order.STATUS.UNKNOWN) {
			sql = sql + " and orders.status = '" + orderQuery.getStatus() + "'";
		}
		if (orderQuery.getLocation() != null
				&& orderQuery.getLocation().getIsNew() != Boolean.TRUE) {
			sql = sql + " and location.id=" + orderQuery.getLocation().getId();
		}
		if (orderQuery.getRequiredDate() != null) {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			sql = sql + " and orders.requiredDate = '"
					+ format.format(orderQuery.getRequiredDate()) + "'";
		}
		sql = sql + " order by orders.id asc";
		Query q = this.em.createNativeQuery(sql, Order.class);
		List<Order> orders = q.getResultList();
		return orders;
	}

}
